package com.jrapider.compent.dict;

import com.jrapider.om.PageInfo;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther: hxs
 * @Date: 2018/12/31
 * @Description:
 */
@Component
@Log4j2
public class DictTranslater {

    private static Map<String, IDictProvider> providerMap = new HashMap<>();
    private static List<DictData> codeDict = new ArrayList<>();

    public static final String DICTTYPE = "DICTTYPE";

    @Autowired
    public void setProviders(List<IDictProvider> providers) {
        for (IDictProvider provider : providers) {
            Set<String> codes = provider.supportCodes();
            for (String code : codes) {
                if (providerMap.containsKey(code)) {
                    log.warn("字典[" + code + "]存在" +
                            providerMap.get(code).getClass().getName() +
                            "," +
                            provider.getClass().getName());
                }
                providerMap.put(code, provider);
            }
        }
        for (String key : providerMap.keySet()) {
            codeDict.add(new DictData() {{
                setValue(key);
                setCode(DICTTYPE);
                setNote(key);
            }});
        }
    }

    public static String getNote(String code, Long value) {
        return getNote(code, value == null ? null : value.toString());
    }

    public static String getNote(String code, String value) {
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(value)) {
            return null;
        }
        List<String> data =
                providerMap.get(code).getDict(code)
                        .stream().filter(dict -> dict.getValue().equals(value))
                        .map(dict -> dict.getNote())
                        .collect(Collectors.toList());
        return data.size() == 0 ? null : data.get(0);
    }

    public static List<String> getNote(String code, List<String> values) {
        if (StringUtils.isEmpty(code) || values.size() == 0) {
            return null;
        }
        List<String> data =
                providerMap.get(code).getDict(code)
                        .stream().filter((dict) -> {
                    for (String value : values) {

                        if (value.equals(dict.getValue())) {
                            return true;
                        }
                    }
                    return false;
                })
                        .map(dict -> dict.getNote())
                        .collect(Collectors.toList());
        return data;
    }

    public static List<DictData> getDict(String code) {
        if(DICTTYPE.equals(code)){
            return codeDict;
        }
        List<DictData> list = providerMap.get(code).getDict(code);
        if (list == null) {
            list = Collections.emptyList();
        }
        return list;

    }

    public static List<DictData> getDict(String code, String filter, PageInfo pageInfo) {
        if(DICTTYPE.equals(code)){
            return codeDict;
        }
        IDictProvider dictProvider = providerMap.get(code);
        if (dictProvider == null) {
            return Collections.emptyList();
        }
        List<DictData> list = dictProvider.getDict(code, filter, pageInfo);
        if (list == null) {
            list = Collections.emptyList();
        }
        return list;

    }

}
